package window;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class CodeWindow extends JFrame {
	
  private static final long serialVersionUID = -5324160085184088010L;
//  private static String entityPackage = "com.sutaitech.beidouhome.component";
//  private static String tableName = "sys_user";
  private static String ftlDescription = "用户表";
  
  private static String entityPackage = "com.paic.mobile.biz.itsSeatRecommentInfo";
  private static String tableName = "its_seat_recommend_info";
  
  private static String entityName = "SysUser";
  private static Integer fieldRowNum = 1;
  private static String primaryKeyPolicy = "uuid";
  private static String sequenceCode = "";


  
  
  String[] planets = { "uuid", "identity", "sequence" };

  JTextField packagefld = new JTextField();
  JLabel showlbl = new JLabel();
  JTextField entityfld = new JTextField();
  JTextField titlefld = new JTextField();
  JTextField tablefld = new JTextField(20);
  JTextField sequence_fld = new JTextField(20);
  JComboBox tablekeyfld = new JComboBox(this.planets);
  JPanel jp = new JPanel();

//  JTextField fieldRowNumfld = new JTextField();
//  JRadioButton jsp = new JRadioButton("Table风格(form)");
//  JRadioButton jsp_row = new JRadioButton("Div风格(form)");
//  JCheckBox actionButton = new JCheckBox("Action");
//  JCheckBox jspButton = new JCheckBox("Jsp");
//  JCheckBox serviceIButton = new JCheckBox("ServiceI");
//  JCheckBox serviceImplButton = new JCheckBox("ServiceImpl");
//  JCheckBox pageButton = new JCheckBox("Page");
//  JCheckBox entityButton = new JCheckBox("Entity");
//  ButtonGroup bg = new ButtonGroup();

  
  public CodeWindow() {
    setContentPane(jp);
    jp.setLayout(new GridLayout(10, 2));

    JLabel infolbl = new JLabel("提示:");
    JLabel packagebl = new JLabel("包名（小写）：");
//    JLabel entitylbl = new JLabel("实体类名（首字母大写）：");
    JLabel tablejbl = new JLabel("表名：");
    JLabel tablekeyjbl = new JLabel("主键生成策略：");
    JLabel sequence_lb = new JLabel("主键SEQUENCE：(oracle序列名)");
    JLabel titlelbl = new JLabel("功能描述：");
//    JLabel fieldRowNumlbl = new JLabel("行字段数目：");
//    fieldRowNumfld.setText(fieldRowNum.toString());

   
    jp.add(infolbl);
    jp.add(showlbl);
    jp.add(packagebl);
    jp.add(packagefld);
//    jp.add(entitylbl);
//    jp.add(entityfld);
    jp.add(tablejbl);
    jp.add(tablefld);

    jp.add(tablekeyjbl);
    jp.add(tablekeyfld);

    jp.add(sequence_lb);
    jp.add(sequence_fld);

    jp.add(titlelbl);
    jp.add(titlefld);
    
//    jsp.setSelected(true);
//    actionButton.setSelected(true);
//    jspButton.setSelected(true);
//    serviceIButton.setSelected(true);
//    serviceImplButton.setSelected(true);
//    pageButton.setSelected(true);
//    entityButton.setSelected(true);
//
//    bg.add(jsp);
//    bg.add(jsp_row);

//    jp.add(fieldRowNumlbl);
//    jp.add(fieldRowNumfld);

//    jp.add(actionButton);
//    jp.add(jspButton);
//    jp.add(serviceIButton);
//    jp.add(serviceImplButton);
//    jp.add(pageButton);
//    jp.add(entityButton);
//
//    jp.add(jsp);
//    jp.add(jsp_row);

    JButton confirmbtn = new JButton("生成");
    

     ActionListener l=new ActionListener() {		
		@Override
		public void actionPerformed(ActionEvent e) {

//				if (!("".equals(packagefld.getText()))) {
//					CodeWindow.entityPackage = packagefld.getText();
//				} else {
//					showlbl.setForeground(Color.red);
//					showlbl.setText("包名不能为空！");
//					return;
//				}
//
////				if (!("".equals(entityfld.getText()))) {
////					CodeWindow.entityName = entityfld.getText();
////				} else {
////					showlbl.setForeground(Color.red);
////					showlbl.setText("实体类名不能为空！");
////					return;
////				}
//
//				if (!("".equals(tablefld.getText()))) {
//					CodeWindow.tableName = tablefld.getText();
//				} else {
//					showlbl.setForeground(Color.red);
//					showlbl.setText("表名不能为空！");
//					return;
//				}
//
//				if (!("".equals(titlefld.getText()))) {
//					CodeWindow.ftlDescription = titlefld.getText();
//				} else {
//					showlbl.setForeground(Color.red);
//					showlbl.setText("描述不能为空！");
//					return;
//				}
//
//				CodeWindow.primaryKeyPolicy = (String) tablekeyfld
//						.getSelectedItem();
//
//				if (CodeWindow.primaryKeyPolicy.equals("sequence")) {
//					if (!("".equals(sequence_fld.getText()))) {
//						CodeWindow.sequenceCode = sequence_fld.getText();
//					} else {
//						showlbl.setForeground(Color.red);
//						showlbl.setText("主键生成策略为sequence时，序列号不能为空！");
//						return;
//					}
//
//				}

		       /* CreateFileProperty createFileProperty = new CreateFileProperty();

		        if (jsp.isSelected()) {
		          createFileProperty.setJspMode("01");
		        }
		        if (jsp_row.isSelected()) {
		          createFileProperty.setJspMode("02");
		        }

		        if (actionButton.isSelected()) {
		          createFileProperty.setActionFlag(true);
		        }
		        if (jspButton.isSelected()) {
		          createFileProperty.setJspFlag(true);
		        }
		        if (serviceIButton.isSelected()) {
		          createFileProperty.setServiceIFlag(true);
		        }
		        if (serviceImplButton.isSelected()) {
		          createFileProperty.setServiceImplFlag(true);
		        }

		        if (entityButton.isSelected()) {
		          createFileProperty.setEntityFlag(true);
		        }*/
		        
		        try
		        {
//		          List<String> list=new ReadTable().readAllTableNames();
//		          System.out.println(list.toString());
//		          String tablename=CodeWindow.tableName.toUpperCase();
//		          System.out.println(tablename);
//		          if(list.contains(tablename)){
//		        	  System.out.println("1111");
//		          }else{
//		        	  System.out.println("222");
//		          }
		          
		          boolean tableexist =true; //new ReadTable().checkTableExist(CodeWindow.tableName);
		          if (tableexist)
		          {
//			        new CodeGenerate().generateToMysqlFile(CodeWindow.entityPackage, CodeWindow.entityName, CodeWindow.tableName, CodeWindow.ftlDescription,  CodeWindow.primaryKeyPolicy, CodeWindow.sequenceCode);
		            new CodeGenerate().generateToOracleFile(CodeWindow.entityPackage, CodeWindow.entityName, CodeWindow.tableName, CodeWindow.ftlDescription,  CodeWindow.primaryKeyPolicy, CodeWindow.sequenceCode);
		            showlbl.setForeground(Color.red);
		            showlbl.setText("成功生成增删改查->功能：" + CodeWindow.ftlDescription); return;
		          }
		          showlbl.setForeground(Color.red);
		          showlbl.setText("表[" + CodeWindow.tableName + "] 在数据库中，不存在");
		          System.out.println(showlbl.getText());
		        }catch (Exception e1){
		          showlbl.setForeground(Color.red);
		          showlbl.setText(e1.getMessage());
		        }
		}
	};
    
    confirmbtn.addActionListener(l);

    JButton extbtn = new JButton("退出");
    extbtn.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e) {
        CodeWindow.this.dispose();
        System.exit(0);
      }
    });
    jp.add(confirmbtn);
    jp.add(extbtn);

    setTitle("代码生成器[单表模型]");
    setVisible(true);
    setDefaultCloseOperation(3);
    setSize(new Dimension(600, 300));
    setResizable(false);
    setLocationRelativeTo(getOwner());
  }

  public static void main(String[] args) {
    try {
      new CodeWindow().pack();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
}